﻿using MediatR;
using Order.Api.Read.Application.Command;
using Order.Api.Read.Dto.Order;
using Order.Api.Read.Service.PrescriptionOrderService;
using Order.ErrorCode;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Order.Api.Read.Application.Handler
{
    /// <summary>
    /// 处方订单分页处理器
    /// </summary>
    public class PrescriptionOrderPageHandler : IRequestHandler<PrescriptionOrderPageCommand, ApiPageIng<PrescriptionOrderPageDto>>
    {
        private readonly IPrescriptionOrderService _prescriptionOrderService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="prescriptionOrderService">处方订单服务</param>
        public PrescriptionOrderPageHandler(IPrescriptionOrderService prescriptionOrderService)
        {
            _prescriptionOrderService = prescriptionOrderService ?? throw new ArgumentNullException(nameof(prescriptionOrderService), "处方订单服务不能为空");
        }

        /// <summary>
        /// 处理处方订单分页查询请求
        /// </summary>
        /// <param name="request">分页查询命令</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>分页结果</returns>
        public async Task<ApiPageIng<PrescriptionOrderPageDto>> Handle(PrescriptionOrderPageCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // 参数验证
                if (request == null)
                {
                    throw new ArgumentNullException(nameof(request), "分页查询请求不能为空");
                }

                // 调用服务层获取分页数据
                var result = await _prescriptionOrderService.GetPrescriptionOrderPageAsync(request);
                return result;
            }
            catch (Exception ex)
            {
                // 记录异常日志
                // TODO: 添加日志记录
                throw new Exception($"处理处方订单分页查询请求时发生错误: {ex.Message}", ex);
            }
        }
    }

    /// <summary>
    /// 处方订单详情处理器
    /// </summary>
    public class PrescriptionOrderDetailHandler : IRequestHandler<PrescriptionOrderDetailCommand, PrescriptionOrderDetailDto>
    {
        private readonly IPrescriptionOrderService _prescriptionOrderService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="prescriptionOrderService">处方订单服务</param>
        public PrescriptionOrderDetailHandler(IPrescriptionOrderService prescriptionOrderService)
        {
            _prescriptionOrderService = prescriptionOrderService ?? throw new ArgumentNullException(nameof(prescriptionOrderService), "处方订单服务不能为空");
        }

        /// <summary>
        /// 处理处方订单详情查询请求
        /// </summary>
        /// <param name="request">详情查询命令</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>处方订单详情</returns>
        public async Task<PrescriptionOrderDetailDto> Handle(PrescriptionOrderDetailCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // 参数验证
                if (request == null)
                {
                    throw new ArgumentNullException(nameof(request), "详情查询请求不能为空");
                }

                // 调用服务层获取详情数据
                var result = await _prescriptionOrderService.GetPrescriptionOrderDetailAsync(request);
                return result;
            }
            catch (Exception ex)
            {
                // 记录异常日志
                // TODO: 添加日志记录
                throw new Exception($"处理处方订单详情查询请求时发生错误: {ex.Message}", ex);
            }
        }
    }
}
